import type { Reducer, Effect } from 'umi';
import type { NoticeIconData } from '@/components/NoticeIcon';
import type { ConnectState } from './connect.d';
import { queryMessage, readMessage } from '@/services/user';

export type NoticeItem = {
  id: string;
  type: string;
  status: string;
} & NoticeIconData;

export type GlobalModelState = {
  collapsed: boolean;
  notices: NoticeItem[];
  msge: NoticeItem[];
};

export type GlobalModelType = {
  namespace: 'global';
  state: GlobalModelState;
  effects: {
    fetchNotices: Effect;
    clearNotices: Effect;
    changeNoticeReadState: Effect;
  };
  reducers: {
    changeLayoutCollapsed: Reducer<GlobalModelState>;
    saveNotices: Reducer<GlobalModelState>;
    saveMsg: Reducer<GlobalModelState>;
    saveClearedNotices: Reducer<GlobalModelState>;
  };
};

const GlobalModel: GlobalModelType = {
  namespace: 'global',

  state: {
    collapsed: false,
    notices: [],
    msge: []
  },

  effects: {
    *fetchNotices({ payload }, { call, put, select }): any {
      const user: any = yield select(
        (state: ConnectState) => state.user.currentUser
      );
      const param: any = {
        pageNo: 1,
        pageSize: 30
      }
      if (user && user.system) {
        if (user.system == 'cm') {
          if (user.selectType == '1') {
            param.type = 'CONSTRUCTION_UNIT';
          } else if (user.selectType == '2') {
            param.type = 'OWNER_INSPECTION';
          } else if (user.selectType == '3') {
            param.type = 'LABOUR_INSPECTION';
          } else {
            param.type = 'CONSTRUCTION_UNIT';
          }
          param.targetId = user.selectCorpId;
        } else if (user.system == 'bk') {
          param.type = 'BANK';
          param.targetId = user.bankId;
        } else if (user.system == 'ls') {
          param.type = 'OA_INSPECTION';
          param.targetId = 0;
        } else if (user.system == 'gs') {
          param.type = 'TP_INSPECTION';
          param.targetId = user.bankId;
        }
      } else {
        return
      }
      if (payload.read) {
        param.read = true
      } else {
        param.read = false
      }
      const res = yield call(queryMessage, param);
      if (payload.read) {
        yield put({
          type: 'saveMsg',
          payload: res.data || [],
        });
      } else {
        yield put({
          type: 'saveNotices',
          payload: res.data || [],
        });
        // const unreadCount: number = yield select(
        //   (state: ConnectState) => state.global.notices.filter((item) => !item.read).length,
        // );
        yield put({
          type: 'user/changeNotifyCount',
          payload: {
            totalCount: res.total,
            unreadCount: res.total
          },
        });
      }
    },
    *clearNotices({ payload }, { put, select }) {
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
      // const count: number = yield select((state: ConnectState) => state.global.notices.length);
      // const unreadCount: number = yield select(
      //   (state: ConnectState) => state.global.notices.filter((item) => !item.read).length,
      // );
      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: 0,
          unreadCount: 0,
        },
      });
    },
    *changeNoticeReadState({ payload }, { call, put, select }): any {

      const res = yield call(readMessage, { id: payload });

      const notices: NoticeItem[] = yield select((state: ConnectState) =>
        state.global.notices.map((item) => {
          const notice = { ...item };
          if (notice.id === payload) {
            notice.read = true;
          }
          return notice;
        }),
      );

      const user: any = yield select(
        (state: ConnectState) => state.user.currentUser
      );

      yield put({
        type: 'saveNotices',
        payload: notices,
      });

      yield put({
        type: 'user/changeNotifyCount',
        payload: {
          totalCount: user.unreadCount - 1,
          unreadCount: user.unreadCount - 1,
        },
      });
    }
  },

  reducers: {
    changeLayoutCollapsed(state = { notices: [], msge: [], collapsed: false }, { payload }): GlobalModelState {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveNotices(state = { notices: [], msge: [], collapsed: false }, { payload }): GlobalModelState {
      return {
        ...state,
        collapsed: false,
        notices: payload,
      };
    },
    saveMsg(state = { notices: [], msge: [], collapsed: false }, { payload }): GlobalModelState {
      return {
        ...state,
        collapsed: false,
        msge: payload,
      };
    },
    saveClearedNotices(state = { notices: [], msge: [], collapsed: false }, { payload }): GlobalModelState {
      return {
        ...state,
        collapsed: false,
        notices: [],//state.notices.filter((item): boolean => item.type !== payload),
      };
    }
  },
};

export default GlobalModel;
